<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Weighdata;
use App\Models\FlowInside;
use App\Utils\ModelOperatorTrait;

class FlowInsideController extends Controller
{
	use ModelOperatorTrait;

    protected function model(){
        return app(\App\Models\Weighdata::class);
    }

    public function getFlowInside(Request $request){

    	$weightdatas = Weighdata::where('vehicle_type','IN')->with(['flowInside'])->get();

    	foreach ($weightdatas as $weightdata) {
    		if($weightdata->flowInside == null && $weightdata->order_systemnumber != null){
    			FlowInside::create([
    				'order_systemnumber' => $weightdata->order_systemnumber
    			]);
    		}
    	}


    	$conditions = $request->all();

    	$conditions['vehicle_type'] = 'IN';

    	$weightdatas = $this->queryModel($conditions,['flowInside','internalVehicle']);

    	if(request()->has('page')){
            $weightdatas = $weightdatas->orderBy('system_datetime','desc')->paginate();
        }
        else{
            $weightdatas = $weightdatas->get();
        }

    	return formatJsonResponse($weightdatas);
    }

    public function updatePurchaseWeight($id,Request $request){
    	$flowInside = FlowInside::where('order_systemnumber',$id)->first();

    	$flowInside->purchase_weight = $request->purchase_weight;

    	$flowInside->save();

    	return formatJsonResponse();
    }

    public function updateDischargeWeight($id,Request $request){

    	$flowInside = FlowInside::where('order_systemnumber',$id)->first();

    	$flowInside->discharge_weight = $request->discharge_weight;

    	$flowInside->save();

    	return formatJsonResponse();
    }

    public function updatePickWeight($id,Request $request){

        $flowInside = FlowInside::where('order_systemnumber',$id)->first();

        $flowInside->pick_weight = $request->pick_weight;

        $flowInside->save();

        return formatJsonResponse();
    }

    public function updateIsReturn($id,Request $request){

    	$flowInside = FlowInside::where('order_systemnumber',$id)->first();

    	$flowInside->is_return = $request->is_return;

    	$flowInside->save();

    	return formatJsonResponse();
    }

    public function updateIsGo($id,Request $request){

    	$flowInside = FlowInside::where('order_systemnumber',$id)->first();

    	$flowInside->is_go = $request->is_go;

    	$flowInside->save();

    	return formatJsonResponse();
    }

    public function updateRemark($id,Request $request){

    	$flowInside = FlowInside::where('order_systemnumber',$id)->first();

    	$flowInside->remarks = $request->remarks;

    	$flowInside->save();

    	return formatJsonResponse();
    }
}
